﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ADDataPro
{
    public partial class FindData : Form
    {

        public FindData()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            button3.Enabled = false;
        }
        object obj = new object();
        object addObj = new object();
        List<IEnumerable<string>> allList = new List<IEnumerable<string>>();
        string[] files;
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = Properties.Settings.Default.LastSelectedFolder;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.LastSelectedFolder = folderBrowserDialog.SelectedPath.ToString();
                Properties.Settings.Default.Save();
                textBox1.Text = folderBrowserDialog.SelectedPath;
                textBox2.Text = string.Empty;
                // 读取 路径下的文件
                files = Directory.GetFiles(textBox1.Text, "*.txt");
                progressBar1.Maximum = files.Length;
                progressBar1.Value = 0;
                button3.Enabled = false;
                textBox3.Text = $"0/{files.Length}";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = false;
            allList.Clear();
            int count = 0;
            var index = comboBox1.SelectedIndex;
            Task.Run(() =>
            {
                foreach (var file in files)
                {
                    List<string> list = new List<string>();
                    using (StreamReader sr = new StreamReader(file))
                    {
                        list.Clear();
                        IEnumerable<string> temp = getList(file, index);
                        list.AddRange(temp);
                        allList.Add(temp);
                        lock (obj)
                        {
                            count++;
                            Invoke(new Action(() =>
                            {
                                textBox3.Text = $"{count}/{files.Length}";
                                progressBar1.Value = count;
                                this.textBox2.AppendText(string.Join("\r\n", list));
                            }));
                        }
                    }
                }
                Invoke(new Action(() =>
                {
                    button2.Enabled = true;
                    button3.Enabled = true;
                }));
            });
        }

        public IEnumerable<string> getList(string file, int index)
        {
            using (StreamReader sr = new StreamReader(file))
            {
                var str = sr.ReadToEnd();
                var arr = str.Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (i % 16 == index)
                    {
                        yield return arr[i];
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string path = $"{ textBox1.Text}";
            string logPath = $"{ path}\\AD{comboBox1.SelectedIndex}_{0.ToString().PadLeft(3, '0')}_Data.csv";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            StreamWriter sw = new StreamWriter(logPath);
            sw.Flush();
            int filecount = 1;
            int count = 0;
            for (int i = 0; i < allList.Count; i++)
            {
                //long.TryParse(allList[i].Item2, out long tickts);
                var enumertor = allList[i].GetEnumerator();
                while (enumertor.MoveNext())
                {
                    var curitem = enumertor.Current;
                    count++;
                    if (count > 1_000_000)
                    {
                        count = 0;
                        sw.Close();
                        sw.Dispose();
                        logPath = $"{ path}\\AD{comboBox1.SelectedIndex}_{filecount.ToString().PadLeft(3, '0')}_Data.csv";
                        filecount++;
                        sw = new StreamWriter(logPath);
                    }
                    sw.WriteLine($"{curitem}");
                }
            }
            sw.Flush();
            sw.Dispose();
            sw.Close();
            allList.Clear();
            MessageBox.Show("保存成功！");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                return;
            }
            var delfiles = Directory.GetFiles(textBox1.Text, "*.csv");
            for (int i = 0; i < delfiles.Length; i++)
            {
                File.Delete(delfiles[i]);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var url = textBox1.Text;
            WaveShow waveShow = new WaveShow(url, comboBox1.SelectedIndex, files);
            waveShow.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var url = textBox1.Text;
            ListDataView listDataView = new ListDataView(url);
            listDataView.ShowDialog();
        }
    }
}
